package com.wms.presenters;

import com.gatech.wms.R;
import com.wms.model.UserModel;

import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;

public abstract class WMSActivity extends Activity {
	public static final int LOGIN_CODE = 101;
	private UserModel mPersistence;
	private Menu actionMenu;
	
	public WMSActivity() {
		mPersistence = UserModel.getInstance();
	}
	
	/**
	 * Creates a context-menu for this screen.
	 * Not needed right now.
	 * @return always true
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main_screen, menu);
		actionMenu = menu;
		if (mPersistence.getAuthenticated())
			menu.getItem(1).setTitle("Logout");
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.item_profile:
                return true;
            case R.id.item_login:
            	Intent loginIntent = new Intent(this, LoginActivity.class);
            	startActivityForResult(loginIntent, LOGIN_CODE);
                return true;
        }
        return false;
    }
	
	@Override
	protected void onActivityResult(int code, int result, Intent data) {
		if (mPersistence.getAuthenticated())
			actionMenu.getItem(1).setTitle("Logout");
		else
			actionMenu.getItem(1).setTitle("Login");
	}
	
	protected UserModel getUserModel() {
		return mPersistence;
	}
}
